let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let wxbarcode = require("../../../utils/wxbarcode/index")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 20,
    listData: [],
  },
  getData() {
    const that = this;
    let data = {
      page: that.data.page,
      limit: that.data.pageSize
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.myHxGiftCoupon, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.hxlist.data.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          that.setData({
            isWithout: true,
            listData: res.data.result.hxlist.data
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }
    })
  },
  scanQr() {
    const that = this;
    wx.scanCode({
      success(res) {
        wx.showModal({
          editable: true,
          placeholderText: '请输入备注',
          title: '是否审核该礼品券',
          success(result) {
            console.log(result, 9999999)
            if (result.confirm) {
              let arr = res.result.split('|');
              let data = {
                type: 1,
                hx_code: arr[0],
                use_token: arr[1],
                hx_mark:result.content
              }
              wx.showLoading({
                title: '加载中...',
              })
              myRequest.EasyHttp.post(api.reqUrls.hxGiftCoupon, data).then(res => {
                if (res.data.status == 1) {
                  that.getData()
                } else {
                  wx.hideLoading();
                  wx.showModal({
                    showCancel: false,
                    content: res.data.msg,
                    success(res) {
                      if (res.confirm) {

                      }
                    }
                  })

                }
              })
              wx.hideLoading();
            }
          }
        })

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })

    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      page: that.data.page + 1,
      limit: that.data.pageSize
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.myHxGiftCoupon, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.hxlist.data.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {

          that.setData({
            listData: [...that.data.listData, ...res.data.result.hxlist.data],
            page: that.data.page + 1
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})